package com.firefly.app.biz.version.service;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.firefly.app.biz.version.dataobject.MobileAppVersionResponse;
import com.firefly.mobile.app.config.Config;
import com.firefly.mobile.app.util.HttpUtil;
import com.firefly.mobile.app.util.Md5;

public class VersionProxyServiceImpl implements VersionProxyService{

	public static String key = Config.getString("version.proxy.key");
	public static String url = Config.getString("version.proxy.url");
	
	
	@Override
	public MobileAppVersionResponse checkVersion(String version) {
		String cpUrl = url+"queryYtgMobileAppVersion";
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("version", version);
		String sign = Md5.md5(version + key);
		paramsMap.put("sign", sign);
		String result = "";
		MobileAppVersionResponse response = new MobileAppVersionResponse();
		try {
			result = HttpUtil.getUrlAsString(cpUrl, paramsMap);
			if (result != null) {
				response = 	JSONObject.parseObject(result, MobileAppVersionResponse.class);
				String fileName  = response.getMobileAppVersionInfo().getFileName();
				String downSign = Md5.md5("version"+fileName+  key);
				String downloadUrl = url+"getFileByName?"+"sign="+downSign+"&path=version"+"&fileName="+URLEncoder.encode(response.getMobileAppVersionInfo().getFileName(), "utf-8");
				response.getMobileAppVersionInfo().setFileName(downloadUrl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}
	
	

	
}
